package common;

import java.util.*;

/**
 * Created by wzl on 19-12-18.
 */
public class RelRecord extends Record{
    private String measurement=null;
    private Map<String,Long> time=null;
    private Map<String,String> idFields=null;
    private Map<String,Float> fields=null;

    public RelRecord(String tableName,Map<String,Long> time, Map<String,String> idFields, Map<String,Float> fields) {
        this.measurement=tableName;
        this.time =time;
        this.idFields=idFields;
        this.fields=fields;
    }

    public String getMeasurement() {
        return measurement;
    }

    public void setMeasurement(String measurement) {
        this.measurement = measurement;
    }

    public Map<String, Long> getTime() {
        return time;
    }

    public void setTimes(Map<String, Long> time) {
        this.time = time;
    }

    public Map<String, String> getIdFields() {
        return idFields;
    }

    public void setIdFields(Map<String, String> idFields) {
        this.idFields = idFields;
    }

    public Map<String, Float> getFields() {
        return fields;
    }

    public void setFields(Map<String, Float> fields) {
        this.fields = fields;
    }

    public String toString(){
        StringBuilder str=new StringBuilder();
        str.append("{");
        Set<String> fieldkeys =fields.keySet();
        for(String key:fieldkeys){
            str.append("\""+key+"\":").append(fields.get(key)).append(",");
        }

        Set<String> idFieldKeys = idFields.keySet();
        for(String key:idFieldKeys){
            str.append("\""+key+"\":").append("\""+idFields.get(key)+"\"").append(",");
        }

        Set<String> keys=time.keySet();
        for(String key:keys) {
            str.append("\""+key+"\":").append(time.get(key));
        }
        str.append("}");
        return str.toString();
    }

    public List<TSRecord> toTSRecordList() {
        List<TSRecord> tsRecordList=new  ArrayList();

        //timestamp
        Set<String> keys =time.keySet();
        long ts=0;
        for(String key:keys){
          ts  = time.get(key);
        }



        Set<String> fieldKeys= fields.keySet();
        for(String key:fieldKeys){
            Map<String,String> tsTags=new HashMap<>();
            Set<String> idFieldKeys =idFields.keySet();
            for(String id:idFieldKeys){
                tsTags.put(id,idFields.get(id));
            }
            tsTags.put("node_id",key);

            Map<String,Float> tsFields=new HashMap<>();
            tsFields.put("value",fields.get(key));

            TSRecord tsRecord=new TSRecord(measurement,ts,tsTags,tsFields);
            tsRecordList.add(tsRecord);
        }

        return  tsRecordList;
    }
}